package com.example.activiti.config.activiti;

import com.example.activiti.config.redis.RedisPool;
import com.example.activiti.config.redis.SerializationUtils;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.impl.persistence.deploy.DeploymentCache;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @author Mr.yuan
 * @version v1.0
 * @date 2019/11/4 17:55
 **/
@Slf4j
public class ActivityDefCache implements DeploymentCache<ProcessDefinitionEntity> {

    private Logger logger = LoggerFactory.getLogger(ActivityDefCache.class);

    private static final String PRE_FIX="act:";

    private final RedisPool redisPool;

    private ActivityDefCache(RedisPool redisPool) {
        this.redisPool = redisPool;
    }


    @Override
    public ProcessDefinitionEntity get(String id) {
        logger.error("从redis获取数据["+ PRE_FIX+id+"]");
        byte[] entityBt = null;
        JedisPool resources = redisPool.getJedisPool();
        Jedis jedis = resources.getResource();
        try {
            entityBt = jedis.hget(PRE_FIX.getBytes(), id.getBytes());
            if(entityBt==null) {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("从redis获取数据失败["+PRE_FIX+id+"]-->"+e.toString());
        }finally{
            redisPool.releaseResource(jedis);
        }
        return (ProcessDefinitionEntity) SerializationUtils.deserialize(entityBt);
    }

    @Override
    public void add(String id, ProcessDefinitionEntity processDefinitionEntity) {
        logger.error("向redis存储数据["+PRE_FIX+id+"]");
        JedisPool resources;
        Jedis jedis = null;
        try {
            resources = redisPool.getJedisPool();
            jedis = resources.getResource();
            jedis.hset(PRE_FIX.getBytes(), id.getBytes(),SerializationUtils.serialize(processDefinitionEntity));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("向redis存储数据失败["+PRE_FIX+id+"]-->"+e.toString());
        }finally{
            redisPool.releaseResource(jedis);
        }
    }

    @Override
    public void remove(String id) {
        logger.error("从redis删除数据["+PRE_FIX+id+"]");
        JedisPool resources;
        Jedis jedis = null;
        try {
            resources = redisPool.getJedisPool();
            jedis = resources.getResource();
            jedis.hdel(PRE_FIX.getBytes(), id.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("从redis删除数据失败["+PRE_FIX+id+"]-->"+e.toString());
        }finally{
            redisPool.releaseResource(jedis);
        }
    }

    @Override
    public void clear() {
        logger.error("从redis删除全部流程数据["+PRE_FIX +"]");
        JedisPool resources;
        Jedis jedis = null;
        try {
            resources = redisPool.getJedisPool();
            jedis = resources.getResource();
            jedis.hdel(PRE_FIX.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("从redis删除全部流程失败["+PRE_FIX +"]-->"+e.toString());
        }finally{
            redisPool.releaseResource(jedis);
        }
    }

}
